namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class UserPositionLevelBLL
    {
        public int Add(UserPositionLevel model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into UserPositionLevel(");
            builder.Append("PositionID,LevelValue");
            builder.Append(") values (");
            builder.Append("@PositionID,@LevelValue");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PositionID", model.PositionID), new SqlParameter("@LevelValue", model.LevelValue) };
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int LevelID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from UserPositionLevel ");
            builder.Append(" where LevelID=@LevelID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LevelID", SqlDbType.Int, 4) };
            cmdParms[0].Value = LevelID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string LevelIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from UserPositionLevel ");
            builder.Append(" where ID in (" + LevelIDlist + ")  ");
            return (DBHelper.ExecuteSql(builder.ToString()) > 0);
        }

        public List<UserPositionLevel> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM UserPositionLevel ");
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            UserPositionLevel item = null;
            List<UserPositionLevel> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<UserPositionLevel>();
                foreach (DataRow row in table.Rows)
                {
                    item = new UserPositionLevel();
                    if (row["LevelID"].ToString() != "")
                    {
                        item.LevelID = int.Parse(row["LevelID"].ToString());
                    }
                    if (row["PositionID"].ToString() != "")
                    {
                        item.PositionID = int.Parse(row["PositionID"].ToString());
                    }
                    item.LevelValue = row["LevelValue"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public UserPositionLevel GetModel(int LevelID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select LevelID, PositionID, LevelValue  ");
            builder.Append("  from UserPositionLevel ");
            builder.Append(" where LevelID=@LevelID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LevelID", SqlDbType.Int, 4) };
            cmdParms[0].Value = LevelID;
            UserPositionLevel level = new UserPositionLevel();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                if (row["LevelID"].ToString() != "")
                {
                    level.LevelID = int.Parse(row["LevelID"].ToString());
                }
                if (row["PositionID"].ToString() != "")
                {
                    level.PositionID = int.Parse(row["PositionID"].ToString());
                }
                level.LevelValue = row["LevelValue"].ToString();
                return level;
            }
            return null;
        }

        public bool Update(UserPositionLevel model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update UserPositionLevel set ");
            builder.Append(" PositionID = @PositionID , ");
            builder.Append(" LevelValue = @LevelValue  ");
            builder.Append(" where LevelID=@LevelID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LevelID", model.LevelID), new SqlParameter("@PositionID", model.PositionID), new SqlParameter("@LevelValue", model.LevelValue) };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

